package com.medical.admin.hospital.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.medical.admin.cost.common.dto.DoctorDto;
import com.medical.admin.cost.entity.Department;
import com.medical.admin.cost.entity.Doctor;
import com.medical.admin.cost.mapper.DepartmentMapper;
import com.medical.admin.cost.mapper.DoctorMapper;
import com.medical.admin.hospital.common.vo.DoctorVo;
import com.medical.admin.hospital.common.vo.DownMenu;
import com.medical.admin.hospital.common.vo.PageVo;
import com.medical.admin.hospital.common.vo.TitleVo;
import com.medical.admin.hospital.entity.Hospital;
import com.medical.admin.hospital.mapper.HospitalMapper;
import com.medical.admin.hospital.service.DoctorService;
import com.medical.admin.sys.entity.Title;
import com.medical.admin.sys.mapper.TitleMapper;
import com.qf.medical.ex.BaseException;
import com.qf.medical.response.ResponseCode;
import com.qf.medical.utils.BeanConvertUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class DoctorServiceImpl implements DoctorService {
    @Resource
    private DoctorMapper doctorMapper;

    @Resource
    private TitleMapper titleMapper;
    @Resource
    private HospitalMapper hospitalMapper;

    @Resource
    private DepartmentMapper departmentMapper;
    /**
     * 下拉展示医生职称
     * @return
     */
    @Override
    public DownMenu findTitleName() {
        //获取医院信息
        List<Title> titleList = titleMapper.selectList(new QueryWrapper<>());
        //判断医院信息是否为空
        Optional.ofNullable(titleList).orElseThrow(()->new BaseException(ResponseCode.TITLE_FIND_ERROR));
        return DownMenu.builder().titles(BeanConvertUtils.convertListTo(titleList, TitleVo::new)).build();
    }

    /**
     * 分页查询医生信息
     * @param departmentName
     * @param doctorName
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageVo<DoctorDto> list(String departmentName, String doctorName, int pageNum, int pageSize) {

        PageVo<DoctorDto> pageVo = new PageVo<>();
        List<DoctorDto> doctorDtoList= doctorMapper.pageDepartments(departmentName,doctorName,(pageNum-1)*pageSize,pageSize);
        pageVo.setRows(BeanConvertUtils.convertListTo(doctorDtoList,DoctorDto::new));
        pageVo.setTotal(doctorDtoList.size());
        return pageVo;

    }

    /**
     * 查询医生信息
     * @param doctorId
     * @return
     */
    @Override
    public DoctorVo select(Long doctorId) {
        Doctor doctor = doctorMapper.selectById(doctorId);
        Optional.ofNullable(doctor).orElseThrow(()-> new BaseException(ResponseCode.DOCTOR_INFO_NOT_EXIT));
        //判断是否被删除
        if (doctor.getState()!=1){
            throw new BaseException(ResponseCode.DOCTOR_INFO_NOT_EXIT);
        }
        DoctorVo doctorVo = BeanConvertUtils.convertTo(doctor, DoctorVo::new);

        Department department = departmentMapper.selectById(doctorVo.getDepartmentId());
        doctorVo.setDepartmentName(department.getDepartmentName());
        Hospital hospital = hospitalMapper.selectById(doctorVo.getHospitalId());
        doctorVo.setHospitalName(hospital.getHospitalName());
        Title title = titleMapper.selectById(doctorVo.getTitleId());
        doctorVo.setTitleName(title.getMarTitleName());
        return doctorVo;
    }

    /**
     * 修改医生信息
     * @param doctorDto
     * @return
     */
    @Override
    public Integer update(DoctorDto doctorDto) {
        //转换对象
        Doctor doctor = BeanConvertUtils.convertTo(doctorDto, Doctor::new);

        //通过医院名称查询id
        Hospital hospital=hospitalMapper.findByHospitalName(doctorDto.getHospitalName());
        doctor.setHospitalId(hospital.getHospitalId());

        //通过科室名称查询id
        Department department = departmentMapper.findByDepartmentName(doctorDto.getDepartmentName());
        doctor.setDepartmentId(department.getDepartmentId());

        //通过职称名称查询id
        Title title = titleMapper.findByDepartmentName(doctorDto.getTitleName());
        doctor.setTitleId(title.getTitleId());

        //新增医生信息
        int count = doctorMapper.updateById(doctor);
        if (count!=1){
            throw new BaseException(ResponseCode.DOCTOR_INFO_UPDATE_ERROR);
        }

        return count;
    }

    /**
     * 新增医生信息
     * @param doctorDto
     * @return
     */
    @Override
    public Integer insert(DoctorDto doctorDto) {

        //转换对象
        Doctor doctor = BeanConvertUtils.convertTo(doctorDto, Doctor::new);

        //通过医院名称查询id
        Hospital hospital=hospitalMapper.findByHospitalName(doctorDto.getHospitalName());
        doctor.setHospitalId(hospital.getHospitalId());

        //通过科室名称查询id
        Department department = departmentMapper.findByDepartmentName(doctorDto.getDepartmentName());
        doctor.setDepartmentId(department.getDepartmentId());

        //通过职称名称查询id
        Title title = titleMapper.findByDepartmentName(doctorDto.getTitleName());
        doctor.setTitleId(title.getTitleId());

        //新增医生信息
        int count = doctorMapper.insert(doctor);
        if (count!=1){
            throw new BaseException(ResponseCode.SYS_ERROR);
        }

        return count;
    }

    /**
     * 删除医生信息
     * @param doctorId
     * @return
     */
    @Override
    public Integer delete(Long doctorId) {
        Doctor doctor = doctorMapper.selectById(doctorId);
        //判断套餐是否存在
        Optional.ofNullable(doctor).orElseThrow(()->new BaseException(ResponseCode.DOCTOR_INFO_NOT_EXIT));
        //判断套餐是否已删除
        if (doctor.getState()!=1){
            throw new BaseException(ResponseCode.DOCTOR_INFO_NOT_EXIT);
        }
        doctor.setState(0);
        int count = doctorMapper.updateById(doctor);
        if (count!=1){
            throw new BaseException(ResponseCode.SYS_ERROR);
        }
        return count;
    }
}
